package com.neu.questionnaireplus.config.session;

import javax.servlet.annotation.WebListener;

import javax.servlet.http.*;

/** 
* @Description: 用于监听用户session对象
* @Param:  
* @return:  
* @Author: ~FHX~ (:TSunS:)
* @Date: 2022/10/26 
*/ 
@WebListener
public class MySessionListener implements HttpSessionListener, HttpSessionAttributeListener {


  @Override
  public void sessionCreated(HttpSessionEvent httpSessionEvent) {
    //System.err.println("session add: " + httpSessionEvent.getSession().getId() + new Date());
  }

  @Override
  public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
    //System.err.println("session destroy: " + httpSessionEvent.getSession().getId() + new Date());
    MySessionContext.delSession(httpSessionEvent.getSession());
  }

  /**
   * @Description: 当向被监听对象中增加一个属性时，web容器就调用事件监听器的attributeAdded方法进行响应，
   * 这个方法接收一个事件类型的参数，监听器可以通过这个参数来获得正在增加属性的域对象和被保存到域中的属性对象
   * @Param: [httpSessionBindingEvent]
   * @return: void
   * @Author: ~FHX~ (:TSunS:)
   * @Date: 2022/10/26
   */

  @Override
  public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
    if (httpSessionBindingEvent.getName().equalsIgnoreCase("admin")) {
      MySessionContext.addSession(httpSessionBindingEvent.getSession());
    }
  }
  /** 
  * @Description:  当删除被监听对象中的一个属性时，web容器调用事件监听器的attributeRemoved方法进行响应
  * @Param: [httpSessionBindingEvent] 
  * @return: void 
  * @Author: ~FHX~ (:TSunS:)
  * @Date: 2022/10/26 
  */ 
  @Override
  public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
    if (httpSessionBindingEvent.getName().equalsIgnoreCase("admin")) {
      MySessionContext.delSession(httpSessionBindingEvent.getSession());
    }
  }

  @Override
  public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {

  }
}

